home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-16 | 8.6 KB | 290 lines | [TEXT/KAHL] |
- /************************************************************
- *
- * Created: Sunday, January 20, 1991 4:11:08 PM
- * CIDIndex.cp
- * C++ class implementation for ID-based index objects
- *
- *
- * Copyright Neologic Systems 1992
- * All rights reserved
- *
- *
- * Within a class, references to permanent objects are kept in a sorted list called an
- * index. The default sorting order in an index is ascending by ID value, however
- * other sorting orders are possible. The class CIDIndex implements the default
- * index.
- *
- ***********************************************************/
-
- #include "NeoTypes.h"
- #include CNeoMetaClassH
- #include CNeoSelectH
- #include CNeoStreamH
- #include CNeoDatabaseH
- #include CNeoInodeH
- #include "CIDIndex.h"
- #include CNeoClassH
-
- #ifndef NeoInherited
- #define NeoInherited CNeoNode
- #endif
-
- /* ****************************************************************** */
- /** Instance Methods **/
- /* ****************************************************************** */
-
- #pragma segment NeoCreate
- CIDIndex::CIDIndex(const short aCount, CNeoNode *aParent, const NeoID aID)
- : CNeoNode(aCount, TRUE, aParent)
- {
- fID = aID;
- }
-
- #ifdef qMacApp
- /*
- * Apple's MPW compiler complains about not being able to automatically generate
- * inline destructors. So we humor it to shut it up.
- */
- #pragma segment NeoDestroy
- CIDIndex::~CIDIndex(void)
- {
- }
- #endif
-
- /*
- * Allocate and initialize a new instance.
- */
- #pragma segment NeoCreate
- CNeoPersist *CIDIndex::New(void)
- {
- CIDIndex * object;
-
- object = new CIDIndex(0, nil, 0);
-
- return object;
- }
-
- // Return the class ID for this kind of object
- #pragma segment NeoInfo
- NeoID CIDIndex::getClassID(void) const
- {
- return kIDIndexID;
- }
-
- /*
- * This method returns the amount of file space the object occupies in the database.
- */
- #pragma segment NeoInfo
- long CIDIndex::getFileLength(void) const
- {
- return kIDIndexFileLength;
- }
-
- /* ****************************************************************** */
- /** I/O Methods **/
- /* ****************************************************************** */
-
- /*
- * The readObject method is used to read in the permanent data members of the
- * object from the given stream. In most cases, this method operates without concern
- * for the type of stream the data values are coming from. As a result, this method
- * is capable of reading from such diverse sources as the data fork of a file, a
- * resource fork, an AppleEvent, the clipboard, a memory block, and so on.
- *
- * The aTag argument indicates how much information to read from the stream.
- * It is sometimes sufficient to read in a minimum amount of data and have all
- * other data be read in on demand. Other times it is necessary to read all the
- * data immediately as the stream may not be available later.
- *
- * The two tag values that are currently used are kNeoObjectTag and kNeoAllTag.
- *
- * The value kNeoObjectTag means that only those values of immediate interest
- * need to be read from the stream, though implementations of readObject may read
- * in additional data member values if they wish.
- *
- * The value kNeoAllTag indicates that all data member values for this class
- * should be read in to the stream.
- */
- #pragma segment NeoRead
- void CIDIndex::readObject(CNeoStream *aStream, const NeoTag aTag)
- {
- short index;
-
- NeoInherited::readObject(aStream, aTag);
-
- for (index = 0; index < fCount; index++)
- fEntry[index].fID = aStream->readLong();
- }
-
- /*
- * The writeObject method is used to write to the permanent data members of the
- * object from the given stream. In most cases, this method operates without concern
- * for the type of stream the data values are coming from. As a result, this method
- * is capable of writing to such diverse sources as the data fork of a file, a
- * resource fork, an AppleEvent, the clipboard, a memory block, and so on.
- *
- * The aTag argument indicates how much information to write to the stream.
- * It is sometimes sufficient to write out only those parts that are dirty. Other
- * times it is necessary to write all the data, as when doing Save As operation
- * or when communicating an object's complete state across and RPC channel.
- *
- * The two tag values that are currently used are kNeoObjectTag and kNeoAllTag.
- *
- * The value kNeoObjectTag means that only those values of immediate interest
- * need to be written to the stream, though implementations of writeObject may
- * write out additional data member values if they wish.
- *
- * The value kNeoAllTag indicates that all data member values for this class
- * should be writen out to the stream.
- */
- #pragma segment NeoWrite
- void CIDIndex::writeObject(CNeoStream *aStream, const NeoTag aTag)
- {
- short index;
-
- NeoInherited::writeObject(aStream, aTag);
-
- for (index = 0; index < fCount; index++)
- aStream->writeLong(fEntry[index].fID);
- }
-
- /* ****************************************************************** */
- /** Object List Management Methods **/
- /* ****************************************************************** */
-
-
- /*
- * The method getObject() reads in the object referred to by the specified entry.
- * It allocates the object in memory then reads the object's state from the database.
- */
- #pragma segment NeoSearch
- CNeoPersist *CIDIndex::getObject(const short aIndex)
- {
- CNeoPersist * object;
- CNeoIDSelect key(fEntry[aIndex].fID);
-
- object = (CNeoPersist *)CNeoClass::FindObject(CNeoMetaClass::FObjClassID, &key, FALSE, nil, nil);
- object->unrefer(); // Our caller will add the reference if it wants to
-
- return object;
- }
-
- /*
- * The method insertObject() fills in the attributes of the new node entry and calls
- * insertEntry() to add it in the proper place in the node.
- */
- #pragma segment NeoAdd
- CNeoNode *CIDIndex::insertObject(const short aIndex, CNeoPersist *aObject)
- {
- NeoID oldClassID;
- CNeoNode * newRoot = nil;
- IDIndexEntry newEntry;
-
- newEntry.fID = aObject->fID;
-
- oldClassID = CNeoMetaClass::FSysClassID;
- CNeoMetaClass::FSysClassID = kIDIndexID;
-
- newRoot = insertEntry(aIndex, (Ptr)&newEntry);
-
- CNeoMetaClass::FSysClassID = oldClassID;
-
- #ifdef qNeoDebug
- if (newRoot)
- NeoVerifyNode(newRoot);
- else
- NeoVerifyNode(this);
- #endif
-
- return newRoot;
- }
-
- /*
- * The KeyManager routine of a node class is a dispatch function. Dispatch functions
- * are capable of performing more than one type of service. The first argument of the
- * function indicates the type of service being requested.
- *
- * At a minimum, all KeyManager functions support the kNeoCanSupport and kNeoGetKey
- * operations. The return value of a kNeoCanSupport request is a Boolean value
- * indicating whether the node class can support a binary search using the given
- * selection criterion. The return value of kNeoGetKey is a select key which can be
- * used to locate the location of the given object in the btree.
- *
- * A pointer to a node class's KeyManager function is usually passed to the
- * constructor of the node class's metaclass object. As such, the KeyManager function
- * is usually called through the metaclass.
- */
- #pragma segment NeoSearch
- void *CIDIndex::KeyManager(const NeoKeyOp aOp, ...)
- {
- va_list argptr; // pointer to argument list.
- long desperate;
- NeoSelectType selectType;
- void * value = nil;
- CNeoSelect * key;
- CNeoPersist * object;
-
- va_start(argptr, aOp);
- switch (aOp) {
- case kNeoCanSupport:
- key = va_arg(argptr, CNeoSelect *);
- selectType = (key ? key->getSelectType() : pNeoID);
- if (selectType == pNeoID)
- value = (void *)(unsigned char)TRUE;
- #ifdef qNeoShare
- else if (selectType == kNeoAEKeySelect &&
- ((CNeoAEKeySelect *)key)->getAEKeyword() == pNeoID)
- value = (void *)(unsigned char)TRUE;
- #endif
- else {
- desperate = va_arg(argptr, long);
- value = NeoInherited::KeyManager(kNeoCanSupport, key, desperate);
- }
- break;
-
- case kNeoGetKey:
- object = va_arg(argptr, CNeoPersist *);
- value = (void *)new CNeoIDSelect(object->fID);
- break;
- }
- va_end(argptr);
-
- return value;
- }
-
- /* ****************************************************************** */
- /** Entry Management Methods **/
- /* ****************************************************************** */
-
- /*
- * This virtual function provides a mechanism for determining the size of entries for
- * a particular node.
- */
- #pragma segment NeoInfo
- short CIDIndex::getEntrySize(void) const
- {
- return sizeof(IDIndexEntry);
- }
-
- #ifdef qNeoDebug
- /* ****************************************************************** */
- /** Debugging Methods **/
- /* ****************************************************************** */
- #pragma segment NeoDebug
- const void *CIDIndex::verify(const void *aValue) const
- {
- short index;
- const IDIndexEntry * entry = (IDIndexEntry *)aValue;
-
- for (index = 0; index < fCount; index++) {
- if (entry)
- entry->fID <= fEntry[index].fID;
- entry = &fEntry[index];
- }
-
- NeoInherited::verify(nil);
-
- return entry;
- }
- #endif
-